@echo off
chcp 65001 >nul

REM Backend service startup script
REM Using conda virtual environment: fetch

echo =========================================
echo Starting Backend Service (conda env: fetch)
echo =========================================

REM Activate conda virtual environment fetch
call conda activate fetch 2>nul
if errorlevel 1 (
    echo Error: Cannot activate conda virtual environment fetch
    echo Please ensure the fetch conda environment has been created
    echo You can create it with: conda create -n fetch python=3.11 -y
    pause
    exit /b 1
)

echo Successfully activated conda environment: fetch

REM Change to backend directory
cd %~dp0backend

echo Current directory: %cd%
echo Checking dependencies...

REM Install dependencies if needed
pip install -r requirements.txt

if %errorlevel% neq 0 (
    echo Error: Failed to install dependencies
    pause
    exit /b 1
)

echo Dependencies check completed, starting service...
echo Backend service will run at: http://127.0.0.1:8000
echo API documentation: http://127.0.0.1:8000/docs
echo Press Ctrl+C to stop the service
echo =========================================
echo.

REM Start FastAPI service
python -m uvicorn app.main:app --reload --host 127.0.0.1 --port 8000

REM If service stops, show message and keep window open
if %errorlevel% neq 0 (
    echo.
    echo Error: Service startup failed
    echo Please check the error messages above
    pause
    exit /b 1
)

REM Keep window open if service stops normally
echo.
echo Backend service has stopped
pause